/*
 * IDesktopClient.java
 *
 * Created on July 25, 2007, 10:07 AM
 */

package com.mar.meclient.bt;

import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import com.mar.meclient.bt.ui.impl.BTPageNavigator;


/**
 *
 * @author  helio
 * @version
 */
public class BTMidlet extends MIDlet {
    private static BTMidlet INSTANCE;
	public static BTMidlet  getInstance(){ return INSTANCE; };
	
	public interface IMidletDestroyListener{
		void midletDestroyed();
	}
	private Vector destroyListeners = new Vector();
	public void addDestroyListener(IMidletDestroyListener mdl){
		destroyListeners.addElement(mdl);
	}
	
    
    /**
     *
     */
    public BTMidlet() {
        INSTANCE = this;
    }
    
    /**
     *
     * @param ex
     */
    public void showAlert(Exception ex) {
        Alert alert = new Alert("Error", ex.getClass().getName() + ": " + ex.getMessage(), null, AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        Display.getDisplay(this).setCurrent(alert);
    }
    
    /**
     *
     * @param ex
     */
    public void showAlert(Exception ex, Displayable nextDisplayable) {
        Alert alert = new Alert("Error", ex.getClass().getName() + ": " + ex.getMessage(), null, AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        Display.getDisplay(this).setCurrent(alert, nextDisplayable);
    }
    
    /**
     *
     * @param message
     */
    public void showAlert(String message) {
        Alert alert = new Alert("Warning", message, null, AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        Display.getDisplay(this).setCurrent(alert);
    }
    
    /**
     *
     */
    public void startApp() {
    	BTPageNavigator btAppPageNavigator = new BTPageNavigator(Display.getDisplay(this));
    	btAppPageNavigator.showStartPage();

//        openCreateNewConnection();
    }
    
    /**
     *
     */
    public void pauseApp() {
        notifyPaused();
    }
    
    /**
     *
     * @param unconditional
     */
    public void destroyApp(boolean unconditional) {
    	for ( int i = 0; i < destroyListeners.size(); i++ ){
    		((IMidletDestroyListener)destroyListeners.elementAt(i)).midletDestroyed();
    	}
        notifyDestroyed();
    }
}
